﻿using System;
using System.Collections.Generic;
using System.Text;
using HistoryToMail;

namespace IM
{
    class IMContact
    {
        public String name;
        public List<IMMessage> messages = new List<IMMessage>();

        public DateTime lastMessageTime;

        public List<IMMessage> getMessagesForHistory()
        {
            List<IMMessage> msgs = null;
            lock (this)
            {
                msgs = messages;
                messages = new List<IMMessage>();
            }
            return msgs;
        }

        public Boolean readyToSend(PluginSettings settings)
        {
            Boolean res = false;
            lock (this)
            {
                res = ((lastMessageTime.Add(new TimeSpan(0, settings.idleTime, 0)) < DateTime.Now)
                       || (settings.maxMsgNumberToSend > 0 && messages.Count >= settings.maxMsgNumberToSend))
                      && (messages.Count > 0);
            }
            return res;
        }


        public void addMessage(String interlocutor, HistoryToMail.MessageType type, String body, DateTime time)
        {
            lock (this)
            {
                IMMessage msg = new IMMessage(type, interlocutor, body, time);
                messages.Add(msg);
                lastMessageTime = time;
            }
        }
    }
}
